import 'package:bot_toast/bot_toast.dart';
import 'package:flutter/material.dart';
import 'package:wallpaper/components/AlertDialog/my_loading.dart';
import 'package:wallpaper/generated/l10n.dart';

class LoadingDialog {
  /// 显示加载弹窗
  ///
  /// [message] 加载提示文本，默认为本地化的 "正在设置壁纸..."
  /// [loadingType] MyLoading 组件的类型，默认为 2
  /// [backgroundColor] 背景颜色，默认为半透明黑色
  ///
  /// 返回一个函数，调用该函数可关闭弹窗
  static VoidCallback showLoading({
    String? message,
    int loadingType = 2,
    Color? backgroundColor,
    BuildContext? context,
  }) {
    // 获取本地化文本，如果context为null则使用默认文本
    final String displayMessage =
        message ?? (context != null ? S.of(context).s3 : '加载中...');

    final cancelFunc = BotToast.showCustomLoading(
      backgroundColor: backgroundColor ?? Colors.black.withOpacity(0.7),
      toastBuilder: (cancel) {
        return Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            MyLoading(type: loadingType),
            const SizedBox(height: 10),
            Text(
              displayMessage,
              style: const TextStyle(
                color: Colors.white,
                fontSize: 14,
              ),
            ),
          ],
        );
      },
    );

    return cancelFunc;
  }

  /// 显示简单的加载弹窗（仅显示加载动画）
  static VoidCallback showSimpleLoading({
    int loadingType = 2,
    Color? backgroundColor,
  }) {
    final cancelFunc = BotToast.showCustomLoading(
      backgroundColor: backgroundColor ?? Colors.black.withOpacity(0.7),
      toastBuilder: (cancel) {
        return MyLoading(type: loadingType);
      },
    );

    return cancelFunc;
  }

  /// 显示自定义内容的加载弹窗
  static VoidCallback showCustomLoading({
    required Widget child,
    Color? backgroundColor,
  }) {
    final cancelFunc = BotToast.showCustomLoading(
      backgroundColor: backgroundColor ?? Colors.black.withOpacity(0.7),
      toastBuilder: (cancel) => child,
    );

    return cancelFunc;
  }
}
